#include "filterEngine.h"

namespace FE {

void generateFilter(const std::string &in, filter &out)
{
	std::stringstream ss (in);
	char cbuffer;
	std::string sbuffer;
	while (ss >> cbuffer) {
		if (cbuffer == '+' || cbuffer == '-') {
			ss >> sbuffer;
			if (cbuffer == '+') {
				out.allow.push_back(sbuffer);
			} else {
				out.disallow.push_back(sbuffer);
			};
		};
	};
};


void applyFilter(const std::vector<LIB::songData> &LIBRARY, const filter &FILTER, playlist &PLAYLIST) {
	std::vector<LIB::songData>::const_iterator lib_itr;
	for (lib_itr = LIBRARY.begin(); lib_itr != LIBRARY.end(); lib_itr++) {
		// for each song in LIBRARY
		bool allow = false, disallow = false;

		std::vector<std::string>::const_iterator tag_itr;
		for (tag_itr = lib_itr->tags.begin(); tag_itr != lib_itr->tags.end(); tag_itr++) {
			// for each tag in the record

			// compares the tag to each tag in the FILTER.allow list
			std::vector<std::string>::const_iterator filter_itr;
			for (filter_itr = FILTER.allow.begin(); filter_itr != FILTER.allow.end(); filter_itr++) {
				std::regex filter_regex (*filter_itr);
				if (std::regex_match(*tag_itr, filter_regex))
					allow = true;
			};

			// compares the tag to each tag in the FILTER.disallow list
			for (filter_itr = FILTER.disallow.begin(); filter_itr != FILTER.disallow.end(); filter_itr++) {
				std::regex filter_regex (*filter_itr);
				if (std::regex_match(*tag_itr, filter_regex))
					disallow = true;
			};
		};

		if (allow && !disallow)
			PLAYLIST.songs.push_back(lib_itr._Ptr);
	};
};

};